<?php

namespace App\Http\Middleware;

use Closure;

class ApiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $api_token = $request->api_token;
        if(empty($api_token)){
            return response()->json(['state' => -100,'token不存在'],JSON_UNESCAPED_UNICODE);
        }
        $user_info = Redis::get($api_token);
        if(empty($user_info)){
            return response()->json(['state' => -101,'权限验证失败'],JSON_UNESCAPED_UNICODE);
        }
        $user_info_arr = json_decode($user_info,true);
        // $user_info_arr = json_decode($user_info, true);
        $request->offsetSet('user_id', $user_info_arr['user_id']);
        $request->offsetSet('user_info',['state' => 200,'msg' => 'success','data' => $user_info_arr]);

        return $next($request);
    }
}